<?php
/*
 * 诚信记录管理
 * @author wanli
 * @time   2012/9/13  
 */
class Controller_Admin_Record extends Controller_Template 
{
	public function __construct ($req, $res)
	{
		$this->template = 'smarty:admin/record/record';
		parent::__construct ($req, $res);
		//$this->_merchant = User::current_user();
	}

	public function action_index ()
	{
		$user_cities = User::cities();
	    if($user_cities){
		   $current_city = current($user_cities);
		   $current_city_id = $current_city['id'];
		}
		$url = '/record/list/all/'.$current_city_id.'/1';
        $this->request->redirect($url);
	}

	/**
	 * 诚信交易记录列表 
	 * @param string $type
	 * @param int    $city_id
	 * @param  int    $page
	 * @return array
	 *
	 */
	public function  action_list($type = 'all',$city_id,$page = 1 )
	{
		   $city_find = $this->city_by_user($city_id);
		   if(!$city_find) throw new SCException('参数错误！');
		   
           if(($type != 'all') && ($type != 'delete'))
			   throw new SCException('参数错误！');
           if($this->request->post()){
		     $search_title = $this->request->post('title');
			 $title=trim($search_title);
			 $page = 1;
		   } 
	       $shop_ids = $this->shop_by_city((int)$city_id,$title);
		   if(empty($shop_ids)){$shop_ids = 0;}

		   $query    = array('shop_id' => $shop_ids,'order_by' => array('created'=>'DESC'));
		   $status = kohana::config('shop.record_status.delete');
		   if($type  == 'delete'){
			 $query['status'] =  'delete';
		   }
           $limit = kohana::config('shop.record_list.limit');
		   $start = $limit*($page-1);
           $start = ($start<=0) ? 0 : $start;
           $record_list  = shop_record::search($query,$start,$limit);
           $record_count   = intval($record_list['count']);
		   $page_total     = ceil($record_count/$limit);
		   $page_config    = array(
				 'current_page'     => array('source'=> 'route','key'=>'param3'),
				 'total_items'      => $record_count,
				 'items_per_page'   => $limit,
			  // 'view'             => 'pagination/basic',
		         'auto_hide'        => true,
		         'first_page_in_url'=> true,
	        );
		     
			$reason_prompt = kohana::config('shop.record_reason_prompt');
			$user_cities = User::cities();
            $pagination = Pagination::factory($page_config);
			$this->template->set('pagination',$pagination->render());
			$this->template->set('user_cities',$user_cities);
            $this->template->set('city_id',$city_id);
            $this->template->set('record_list',$record_list['list']);
            $this->template->set('reason_prompt',$reason_prompt);
			$this->template->set('count',$record_count);
			$this->template->set('current_time',time());
			$this->template->set('type',$type);
	}
	
	/**
	 *  判定$city_id 是否在用户的编辑权限内
	 */
	private function city_by_user($city_id)
	{
  	      $user_cities = User::cities();
		  $city_find = false;
		  foreach($user_cities as $value)
		  {
			if($city_id == $value['id'])
		    {
				$city_find = true;
				break;
			}
		  }
		  return $city_find;
	}

	/**
	 * 根据$city_id 获取所有开通店铺ids
	 * @return array
	 */
    private function shop_by_city($city_id,$shop_name=null)
	{
	      $query     = array('city'=>$city_id,'shop_status'=> "open");
		  if($shop_name){
		   $query['shop_name'] = $shop_name;
		  }
		  $shop_info = Shop::search($query);
		  $shop_ids = array();
		  if($shop_info['list'])
		  {
		     foreach($shop_info['list'] as $value)
			 {
			    $shop_ids[] = $value['id'];
			 }
		  }
		  return $shop_ids;
	}

	/**
     *  设置指定的诚信交易记录信息
	 *  @param string $type delete | recover
	 *  @param int    $record_id 
	 *  @param int    $city_id
	 *
	 */
	public function action_set($type,$record_id,$city_id)
	{ 
	  $status = $this->city_by_user($city_id);
	  if(!$status){
	   $this->chg_url(); 
	  }
	  $data = array(
	    'admin_uid' => user::current_user_id(),
		'dealtime'  => time(),		
	  );
	  if($type == 'delete'){
	    $data['status'] = kohana::config("shop.record_status.delete"); 
		$data['reason'] = (int)$this->request->post('reason');
	  }
	  if($type == 'recover'){
	    $data['status'] = kohana::config("shop.record_status.normal");	
	    $data['reason'] = kohana::config("shop.record_reason.normal");  
	  }
	  if($this->request->post()){
	    $record_id = (int)$this->request->post('record');
	  }
	  if(($type == 'recover') || ($type == 'delete'))
	  {
	     shop_record::set_record_status($record_id,$data);
	  }
      $this->chg_url();

	}

	/**
	 * 跳转url
	 *  
	 */
	private function chg_url()
	{
		
	   // 返回页面
	   $url       = $_SERVER["HTTP_REFERER"];
	   if(!$url){
	   $url = '/record/index';
	   }
	   $this->request->redirect($url);
	}
}
	
